package router

import (
	"admin-system/internal/handler"
	"admin-system/internal/middleware"

	"github.com/gin-gonic/gin"
)

// Setup 设置路由
func Setup(r *gin.Engine) {
	// 创建处理器
	authHandler := handler.NewAuthHandler()
	userHandler := handler.NewUserHandler()
	roleHandler := handler.NewRoleHandler()
	menuHandler := handler.NewMenuHandler()
	permissionHandler := handler.NewPermissionHandler()

	// 公共路由
	r.POST("/api/auth/login", authHandler.Login)

	// 需要认证的路由
	api := r.Group("/api")
	api.Use(middleware.Auth())
	{
		// 认证相关
		api.GET("/auth/info", authHandler.GetUserInfo)
		api.GET("/auth/menus", authHandler.GetUserMenus)
		api.POST("/auth/refresh", authHandler.RefreshToken)
		api.POST("/auth/logout", authHandler.Logout)
		api.POST("/auth/change-password", authHandler.ChangePassword)

		// 用户管理
		api.GET("/users", middleware.Permission("user:list"), userHandler.List)
		api.GET("/users/:id", middleware.Permission("user:list"), userHandler.Get)
		api.POST("/users", middleware.Permission("user:create"), userHandler.Create)
		api.PUT("/users/:id", middleware.Permission("user:update"), userHandler.Update)
		api.DELETE("/users/:id", middleware.Permission("user:delete"), userHandler.Delete)
		api.POST("/users/:id/reset-password", middleware.Permission("user:update"), authHandler.ResetPassword)

		// 角色管理
		api.GET("/roles", middleware.Permission("role:list"), roleHandler.List)
		api.GET("/roles/all", middleware.Permission("role:list"), roleHandler.GetAll)
		api.GET("/roles/:id", middleware.Permission("role:list"), roleHandler.Get)
		api.POST("/roles", middleware.Permission("role:create"), roleHandler.Create)
		api.PUT("/roles/:id", middleware.Permission("role:update"), roleHandler.Update)
		api.DELETE("/roles/:id", middleware.Permission("role:delete"), roleHandler.Delete)
		api.PUT("/roles/:id/status", middleware.Permission("role:update"), roleHandler.UpdateStatus)
		api.PUT("/roles/:id/permissions", middleware.Permission("role:update"), roleHandler.UpdatePermissions)
		api.PUT("/roles/:id/menus", middleware.Permission("role:update"), roleHandler.UpdateMenus)
		api.GET("/roles/:id/permissions", middleware.Permission("role:list"), roleHandler.GetRolePermissions)
		api.GET("/roles/:id/menus", middleware.Permission("role:list"), roleHandler.GetRoleMenus)

		// 菜单管理
		api.GET("/menus", middleware.Permission("menu:list"), menuHandler.List)
		api.GET("/menus/tree", middleware.Permission("menu:list"), menuHandler.Tree)
		api.GET("/menus/all", middleware.Permission("menu:list"), menuHandler.GetAll)
		api.GET("/menus/all/tree", middleware.Permission("menu:list"), menuHandler.GetAllTree)
		api.GET("/menus/:id", middleware.Permission("menu:list"), menuHandler.Get)
		api.POST("/menus", middleware.Permission("menu:create"), menuHandler.Create)
		api.PUT("/menus/:id", middleware.Permission("menu:update"), menuHandler.Update)
		api.DELETE("/menus/:id", middleware.Permission("menu:delete"), menuHandler.Delete)
		api.PUT("/menus/sort", middleware.Permission("menu:update"), menuHandler.UpdateSort)

		// 权限管理
		api.GET("/permissions", middleware.Permission("permission:list"), permissionHandler.List)
		api.GET("/permissions/all", middleware.Permission("permission:list"), permissionHandler.GetAll)
		api.GET("/permissions/:id", middleware.Permission("permission:list"), permissionHandler.Get)
		api.POST("/permissions", middleware.Permission("permission:create"), permissionHandler.Create)
		api.PUT("/permissions/:id", middleware.Permission("permission:update"), permissionHandler.Update)
		api.DELETE("/permissions/:id", middleware.Permission("permission:delete"), permissionHandler.Delete)
		api.PUT("/permissions/:id/status", middleware.Permission("permission:update"), permissionHandler.UpdateStatus)
	}
}
